$(function(){
	$("#userDetailPhone").parent().click(function(){
		var callPhone = $("#userDetailPhone").html();
		if(confirm("确定要拨打电话联系Ta吗")){
			cordova.exec(function(data){xfAlert("success");}, function(w){xfAlert("失败");}, "makePhoneCall", "call", [callPhone]);
		}
	});
	$("#userDetailQQ").parent().click(function(){
		if(confirm("确定打开qq吗")){
			cordova.exec(function(data){xfAlert("success");}, function(w){xfAlert(w)}, "openOtherApp", "qq", ["123456"]);
		}
	});
	$("#toUserDetail").click(function(){
		getUserRank($(this).attr("userid"));
		getOtherBook($(this).attr("rid"));
	});
	$("#toBookSubDetail").click(function(){
		bookSumary($(this).attr("isbn"),$("#contentText"));
	});
	$("a[href='#bookLocation']").click(function(){
		$("#bookLocationMap img").attr("src","getLoadingLi('地图加载中...')");
		var lng = $(this).attr("lng");
		var lat = $(this).attr("lat");
		
		var width = window.screen.width;
		var hegiht = window.screen.height;
		if(width>1023||hegiht>1023){
			width = width/2;
			hegiht = hegiht/2;
		}
		var mapUrl = "http://api.map.baidu.com/staticimage?markers="+lng+","+lat+"&center="+lng+","+lat+"&width="+width+"&height="+hegiht+"&zoom=14";
		$("#bookLocationMap img").attr("src",mapUrl);
	});
});

function commentNum(isbn){
	var url ="http://api.douban.com/book/subject/isbn/"+isbn+"/reviews";
	$.ajax({
		url : url,
		data:{'alt':'xd'},
		dataType : "jsonp",
		beforeSend:function(data){
			$('#toBookComment > p').html('加载中...');
			$("#commentList").html(getLoadingLi("拼命加载中..."));
		},
		success:function(json){
			$('#toBookComment > p').html('读者书评（'+json.entry.length+'）');
			showDoubanComment(json);
		}
	});
}

function releaseDetail(data){
	
	$("img#selectedPic").attr("src","images/default1.jpg");
	$("img#selectedPic").attr("isbn",data.attr("isbn"));
	coverCover();
	$("#bookDetailTitle").html(data.attr("title"));
	$("#bookDetailAuthor").html(data.attr("author"));
	$("#bookDetailAddress").html(data.attr("address"));
	commentNum(data.attr("isbn"));
	//otherInfo
	$(".otherTitle").html("书名："+data.attr("title"));
//	$(".otherAuthor").html("作者："+data.attr("author"));
	if(data.attr("author")==undefined||data.attr("author")==""){
		$(".otherAuthor").html("作者：暂无信息");
	}else{
		$(".otherAuthor").html("作者："+data.attr("author"));
	}
	
	if(data.attr("publisher")==undefined||data.attr("publisher")==""){
		$(".otherPublisher").html("出版社：暂无信息");
	}else{
		$(".otherPublisher").html("出版社："+data.attr("publisher"));
	}
	
	$(".otherIsbn").html("isbn："+data.attr("isbn"));
	if(data.attr("class0")==undefined){
		$(".otherClass").html("类型：暂无信息");
	}else{
		$(".otherClass").html("类型："+data.attr("class0"));
	}
	//uesrDetail
	$("#userDetailNickname").html(data.attr("userNickname"));
	$("#userDetailPhone").html(data.attr("phone"));
	if(data.attr("qq")==undefined||data.attr("qq")==""){
		$("#userDetailQQ").html("ta没有留下qq");
	}else{
		$("#userDetailQQ").html(data.attr("qq"));
	}
	//other
	$("#toBookSubDetail").attr("isbn",data.attr("isbn"));
	$("#toUserDetail").attr("userid",data.attr("userid"));
	$("#toUserDetail").attr("rid",data.attr("id"));
	//booklocale
	$("a[href='#bookLocation']").attr("lng",data.attr("lng"));
	$("a[href='#bookLocation']").attr("lat",data.attr("lat"));
	
}

function getUserRank(userid){
	
	$.ajax({
		url :interfaceMobileUrl+"/getUserRank.json?userid="+userid,
		jsonp: "callback",
	    dataType: "jsonp",
//		error:function(){
//			xfAlert("是不是网络不通啊");
//		},
		beforeSend:function(data){
			$("#userRankList").html("");
			$("#userRankList").append(getLoadingLi("正在加载中..."));
		},
		error:function(data){
			$("#userRankList").html("");
			$("#userRankList").append(getNoneDataLi("网络连接失败..."));
		},
		success:function(data){
			var userRank = eval('(' + data + ')'); 
			userRank = userRank.list[0]["com.opac.readerHome.domain.UserComment"];
			$("#userRankList").html("");
			if(userRank == undefined){
				$("#userRankList").append(getNoneDataLi("暂无评价"));
				return ;
			}
			if(userRank.length == undefined){
				userRank = [userRank];
			}
			for(var i=0;i<userRank.length;i++){
				var desRank = userRank[i].desRank;
				var timeRank = userRank[i].timeRank;
				var fromNickname = userRank[i].fromNickname;
				var duetime = userRank[i].duetime["$"];
				var li="<li class='zzrpj_xx'><div class='zjr_pf'><p>守信守时<span>"+timeRank+"</span></p><ul>*timeRankReplace*</ul></div><div class='zjr_pf'><p>描述相符<span>"+desRank+"</span></p><ul>*desRankReplace*</ul></div><div class='pjzjrrw'><img src='images/toux.jpg'><p>"+fromNickname+"</p><p>"+duetime+"</p></div></li>";
				//replace timeRankStar/
				var timeRankStar = "";
				for(var j=0;j<timeRank;j++){
					timeRankStar += " <li class='cur'></li>";
				}
				for(var j=0;j<5-timeRank;j++){
					timeRankStar += " <li></li>";
				}
				li = li.replace("*timeRankReplace*",timeRankStar);
				//replace desRankStar
				var desRankStar = "";
				for(var j=0;j<desRank;j++){
					desRankStar += " <li class='cur'></li>";
				}
				for(var j=0;j<5-desRank;j++){
					desRankStar += " <li></li>";
				}
				li = li.replace("*desRankReplace*",desRankStar);
				
				$("#userRankList").append(li);
			}
		}
	});
	
	$.ajax({
		url :interfaceMobileUrl+"/getRankUser.json?userid="+userid,
		jsonp: "callback",
		dataType: "jsonp",
		jsonpCallback:"getRankUser",
		success:function(data){
//			getRankUser(data);
		}
	});
}

function getRankUser(data){
	var rankUser = eval('(' + data + ')')["com.opac.readerHome.domain.User"];
	//向下取整
	var desRank = rankUser.desRank;
	var timeRank = rankUser.timeRank;
	
	var zzrpf_xf=$('.zzrpf div').eq(0);
	var zzrpf_ss=$('.zzrpf div').eq(1);
	if(desRank==undefined||desRank==undefined){
		$(zzrpf_xf).find('span').html("暂无评价");
		$(zzrpf_ss).find('span').html("暂无评价");
		return;
	}else{
		$(zzrpf_xf).find('span').html(desRank);
		$(zzrpf_ss).find('span').html(timeRank);
		zzrpf_xf = $(zzrpf_xf).find('li').attr('class','');
		zzrpf_ss = $(zzrpf_ss).find('li').attr('class','');
	}
	desRank = Math.floor(rankUser.desRank);
	timeRank = Math.floor(rankUser.timeRank);
	$.each(zzrpf_xf,function(i,xf){
			$(xf).removeClass('cur');
	});
	$.each(zzrpf_ss,function(i,ss){
			$(ss).removeClass('cur');
	});
	$.each(zzrpf_xf,function(i,xf){
		if(desRank>i){
			$(xf).addClass('cur');
		}
	});
	$.each(zzrpf_ss,function(i,ss){
		if(timeRank>i){
			$(ss).addClass('cur');
		}
	});
}